package cn.song.controller;

import cn.song.Utils.ResultUtil;
import cn.song.VO.ProductInfoVo;
import cn.song.VO.ProductVo;
import cn.song.VO.ResultVo;
import cn.song.domain.ProductCategory;
import cn.song.domain.ProductInfo;
import cn.song.service.CategoryService;
import cn.song.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/buyer/product")
public class ProductBuyerController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVo<ProductVo> BuyerProductController(){

        //查询在线商品
        List<ProductInfo> productInfoList = productService.findUpAll();

        // 查询条目
        List<Integer> integerList = productInfoList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findProductCategoryByType(integerList);

        //拼接数据
        List<ProductVo> productVOList = new ArrayList<>();
        for(ProductCategory productCategory :productCategoryList){
            ProductVo productVo = new ProductVo();
            productVo.setCategoryType(productCategory.getCategoryType());
            productVo.setCategoryName(productCategory.getCategoryName());
            List<ProductInfoVo> productInfoVOList = new ArrayList<>();
            for(ProductInfo productlInfo :productInfoList){
                if(productlInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties( productlInfo,productInfoVo);
                    productInfoVOList.add(productInfoVo) ;
                }
            }
            productVo.setProductInfoVoList(productInfoVOList);
            productVOList.add(productVo);
        }

        return ResultUtil.success(productVOList);

    }
}
